package org.redis.cluster.interceptor.support;

import java.util.Collection;
import java.util.concurrent.Callable;

import org.redis.cluster.RedisCluster;
import org.redis.cluster.extension.ExtensionLoader;
import org.redis.cluster.interceptor.Interceptor;
import org.redis.cluster.interceptor.Invocation;
import org.redis.cluster.interceptor.InvocationFactory;
import org.redis.cluster.plugin.Plugin;
import org.redis.cluster.redis.RedisCommand;
import org.redis.cluster.redis.transport.RedisReply;
import org.redis.cluster.transport.session.Session;

public class DefaultInvocationFactory implements InvocationFactory,Plugin {
	
	private RedisCluster redisCluster;
	private ExtensionLoader extensionLoader;
	
	private Collection<Interceptor> interceptors;
	
	@Override
	public void init(RedisCluster redisCluster) {
		this.redisCluster=redisCluster;
		this.extensionLoader=redisCluster.getExtensionLoader();
		this.interceptors=initInterceptors();
	}
	
	@Override
	public void destroy() {
	}

	@Override
	public Invocation getInvocation(Session session, RedisCommand command,Callable<RedisReply> callback) {
		return new DefaultInvocation(session, command, interceptors,callback);
	}
	
	protected Collection<Interceptor> initInterceptors() {
		return extensionLoader.load(Interceptor.class);
	}

}